'use strict';

var ad = appData();
//@ sourceURL=socket.js

// var Socket = function () {
//   this.server = io.connect(appApi("SOCKET_URL"));
//   this.login({user: "suibian"});
// };

// Socket.prototype = {
//   login (obj) {
//     this.server.emit('login', obj);
//   },
//   send (obj) {
//     this.server.emit('log', obj);
//   },
//   receive (callback) {
//     this.server.on('log', function (data) {
//       if (callback) {
//         callback(data);
//       }
//     });
//   }
// };

var Socket = function Socket() {
  this.ws = new WebSocket(appApi("SOCKET_URL"));
  this.connect();
  this.ws.onclose = function (event) {
    console.log('Info: connection closed.');
    console.log(event);
  };
};

Socket.prototype = {
  connect: function connect() {
    this.ws.onopen = function () {
      console.log('Info: connection success.');
    };
  },
  send: function send(obj) {
    this.ws.send(obj);
  },
  receive: function receive(callback) {
    this.ws.onmessage = function (event) {
      if (callback) {
        callback(JSON.parse(event.data));
      }
    };
  }
};

window.$io = new Socket();